<?php
//require_once 'CoreReportingApiReference.php';
$profile=$_SESSION['profileid'];

function getRows($results) {
    $table = 'Rows Of Data';

    if (count($results->getRows()) > 0) {
      $table .= '<table border="1" cellspacing="1" cellpadding="5">';

      // Print headers.
      $table .= '<tr>';

      foreach ($results->getColumnHeaders() as $header) {
        $table .= '<th>' . $header->name . '</th>';
      }
      $table .= '</tr>';

      // Print table rows.
      foreach ($results->getRows() as $row) {
        $table .= '<tr>';
          //foreach ($row as $cell) {
            $table .= '<td align="left">'
                      .$row[0]. //htmlspecialchars($cell[0], ENT_NOQUOTES)
                    '</td>';
			$table .= '<td align="right">'
                      .$row[1]. //htmlspecialchars($cell[1], ENT_NOQUOTES)
                    '</td>';
          //}
        $table .= '</tr>';
      }
      $table .= '</table>';

    } else {
      $table .= '<p>No results found.</p>';
    }

    return $table;
  }
function getRow($results, $table, $th1, $th2) {
$table;

if (count($results->getRows()) > 0) {
  $table .= '<table border="1" cellspacing="1" cellpadding="5">';


  $table .= '<tr>';

	$table .= '<th>'.$th1.'</th>';
	$table .= '<th>'.$th2.'</th>';

  $table .= '</tr>';
  foreach ($results->getRows() as $row) {
 
	$table .= '<tr>';
	$table .= '<td align="left">'
                      .$row[0].
                    '</td>';
			$table .= '<td align="right">'
                      .$row[1]. 
                    '</td>';
	$table .= '</tr>';
  }
  $table .= '</table>';

} else {
  $table .= '<p>No results found.</p>';
}

    return $table;
  }
?>

<div class = "row" >
<?php
//echo "<h3>unique visitors</h3>";
$UVParams = array(
      'dimensions' => 'ga:date',
      
      
		);
$resultsUV = $analytics->data_ga->get(
       'ga:' . $profile,
       '2014-11-01',
       '2014-11-15',
	   'ga:newUsers',
       $UVParams
	   );
	   
//$demo = getRows($resultsUV);
//print $demo;

$th1 = "Tanggal";
$th2 = "Jumlah pengunjung baru";
$table = "Unique Visitors";
$demo = getRow($resultsUV, $table, $th1, $th2);
print $demo;
	   


?>
</div>

<div class = "row" >
<?php
//echo "<h3>Average Time per Visit</h3>";
$ATParams = array(
      'dimensions' => 'ga:month',
      
      
      'max-results' => '25');
$resultsAT = $analytics->data_ga->get(
       'ga:' . $profile,
       '2014-01-01',
       '2014-11-15',
	   'ga:avgSessionDuration',
       $ATParams
	   );
	   
//$demo = getRows($resultsAT);
//print $demo;

$th1 = "Bulan";
$th2 = "Durasi Rata2 Ketika berkunjung (detik)";
$table = "Average Time per Visit";
$demo = getRow($resultsAT, $table, $th1, $th2);
print $demo;	   
?>
</div>

<div class = "row" >
<?php
echo "<h3>Repeat Visitor Percentage</h3>";
$RVParams = array(
      'dimensions' => 'ga:userType',
      'filters' => 'ga:userType==Returning Visitor'
     
		);

$resultsRV = $analytics->data_ga->get(
       'ga:' . $profile,
       '2014-11-01',
       '2014-11-15',
	   'ga:users',
       $RVParams
	   );
	   
$resultsTV = $analytics->data_ga->get(
       'ga:' . $profile,
       '2014-11-01',
       '2014-11-15',
	   'ga:users'
	   );
	   

   if (count($resultsTV->getRows()) > 0) {
      
	foreach ($resultsTV->getRows() as $tv)
      {
		$total =  $tv[0];
       
      }
    

    } else {
      echo '<p>No results found.</p>';
    }

   if (count($resultsRV->getRows()) > 0) {
      
	foreach ($resultsRV->getRows() as $rv)
      {
		$hasil =  $rv[1]/$total;
       
       echo "Persentase kunjungan oleh pengunjung yang pernah mengakses website adalah ".$hasil."%";
      }
    

    } else {
      echo '<p>No results found.</p>';
    }
	   


?>
</div>

<div class = "row" >
<?php
echo "<h3>Frequency</h3>";
$FParams = array(
      'dimensions' => 'ga:date',
      'filters' => 'ga:userType==New Visitor'
     
		);
$resultsF = $analytics->data_ga->get(
       'ga:' . $profile,
       '2014-11-01',
       '2014-11-15',
	   'ga:sessions',
       $FParams
	   );
	   
//$demo = getRows($resultsF);
//print $demo;
	   
$th1 = "Tanggal";
$th2 = "Kunjungan oleh pengunjung baru";
$table = "Frequency";
$demo = getRow($resultsF, $table, $th1, $th2);
print $demo;	   

?>
</div>

<div class = "row" >
<?php
echo "<h3>Recency</h3>";
$RParams = array(
      'dimensions' => 'ga:daysSinceLastSession'
      
      
		);
$resultsR = $analytics->data_ga->get(
       'ga:' . $profile,
       '2014-11-01',
       '2014-11-15',
	   'ga:users',
       $RParams
	   );
	   
$rlength = count($resultsR->getRows());

if (count($resultsR->getRows()) > 0) {
    $newr = array(); 
	foreach ($resultsR->getRows() as $r)
      {
		$newr[] =  $r[0];
       
       //echo $total;
      }
    
	
	
	$hasil = array_sum($newr)/$rlength;
	echo "Rentang waktu pengunjung dalam mengakses website adalah ".$hasil." Hari";
    } else {
      echo '<p>No results found.</p>';
    }


?>
</div>

<div class = "row" >
<?php
echo "<h3>Stickiness</h3>";
/* $UVParams = array(
      'dimensions' => 'ga:date',
      
      
		);
$resultsUV = $analytics->data_ga->get(
       'ga:' . $profile,
       '2014-11-01',
       '2014-11-15',
	   'ga:newUsers',
       $UVParams
	   );
	   
$demo = getRows($resultsUV);
print $demo; */
	   


?>
</div>


